//
//  Searchdatabase.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/29.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "Searchdatabase.h"

@implementation Searchdatabase

+ (void)insertWithString:(NSString *)string
{
    NSString *str = [NSString stringWithFormat:@"insert into Liet(song_name,singer_name)values('%@','%@')", string, string];
    sqlite3 *db = [SearchDB openDB];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}

+ (void)insertWithmodel:(SearchModel *)model
{
    NSString *str = [NSString stringWithFormat:@"insert into Liet(song_name,singer_name)values('%@','%@')",model.song_name,model.singer_name];
    sqlite3 *db = [SearchDB openDB];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}

+ (void)deleteWithModel:(SearchModel *)model
{
    NSString *str = [NSString stringWithFormat:@"delete from Liet where singer_name = '%@'",model.singer_name];
    sqlite3 *db = [SearchDB openDB];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}


+ (void)updateWithModel:(SearchModel *)model
{
    NSString *str = [NSString stringWithFormat:@"updete from Liet set singer_name = %@",model.singer_name];
    sqlite3 *db = [SearchDB openDB];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"更新成功");
    }else{
        NSLog(@"更新失败");
    }
    
}


+ (NSArray *)selectAll
{
    NSString *str = @"select song_name,singer_name from Liet";
    sqlite3 *db = [SearchDB openDB];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char * asong_name = sqlite3_column_text(stmt, 0);
            const unsigned char * asinger_name = sqlite3_column_text(stmt, 1);
            
//            const unsigned char * aalume_name = sqlite3_column_text(stmt, 2);
//            const unsigned char * aurl = sqlite3_column_text(stmt, 3);
//            const unsigned char * apic_url = sqlite3_column_text(stmt, 4);
//            const unsigned char * apicUrl = sqlite3_column_text(stmt, 5);
            
            
            NSString *bsong_name = [NSString stringWithUTF8String:(const char *)asong_name];
            NSString *bsinger_name = [NSString stringWithUTF8String:(const char *)asinger_name];
//            NSString *balume_name = [NSString stringWithUTF8String:(const char *)aalume_name];
//            NSString *burl = [NSString stringWithUTF8String:(const char *)aurl];
//            NSString *bpic_url = [NSString stringWithUTF8String:(const char *)apic_url];
//            NSString *bpicUrl = [NSString stringWithUTF8String:(const char *)apicUrl];
            
            SearchModel *model = [SearchModel modelWithsong_name:bsong_name singer_name:bsinger_name];
            [array addObject:model];
                                  
        }
    }
    return array;
}


//+ (SearchModel *)selectWithsong_name:(NSString *)song_name
//{
//    NSString *str = [NSString stringWithFormat:@"select song_name,singer_name from Liet where song_name = %@",song_name];
//    sqlite3 *db = [SearchDB openDB];
//    
//    sqlite3_stmt *stmt = nil;
//    
//    
//    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
//    
//    if (result == SQLITE_OK) {
//        if (sqlite3_step(stmt) == SQLITE_ROW) {
//            const unsigned char *song_name = sqlite3_column_text(stmt, 0);
//            
//            NSString *asong_name = [NSString stringWithUTF8String:(const char *)song_name];
//            
//            SearchModel *model = [SearchModel modelWithsong_name:asong_name singer_name:<#(NSString *)#>]
//            
//        }
//    }
//}






@end
